package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

@RestController("AdminController")
@Slf4j
@Api(tags = "管理端订单管理")
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 根据ID拒接订单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    public Result reject(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        orderService.reject(ordersRejectionDTO);
        return Result.success();
    }


    /**
     * 根据ID取消订单
     *
     * @param cancelDTO
     * @return
     */
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO cancelDTO) {
        orderService.cancel(cancelDTO);
        return Result.success();
    }


    /**
     * 接单
     *
     * @param
     * @return
     */
    @ApiOperation("商家接单")
    @PutMapping("/confirm")
    public Result takeOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {

        Orders orders = orderService.getById(ordersConfirmDTO);
        if (orders == null) {
            log.info("订单不存在");
            return Result.error(MessageConstant.ORDER_NOT_FOUND);
        }
        if (orders.getStatus() == 2) {
            orderService.takeOrder(ordersConfirmDTO);
            return Result.success();
        }
        log.info("状态异常");
        return Result.error(MessageConstant.ORDER_STATUS_ERROR);
    }

    /**
     * 订单搜索
     *
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> searchOrder(OrdersPageQueryDTO dto) {

        PageResult pageResult = orderService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 统计订单数量
     * @return
     */
    @ApiOperation("统计各状态数量")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> countStatistics(){
        OrderStatisticsVO orderStatisticsVO= orderService.count();
        return Result.success(orderStatisticsVO);
    }
}

